package sse.ustc.cc.fragment;

import java.util.ArrayList;
import java.util.List;

import sse.ustc.cc.activity.R;
import sse.ustc.cc.adapter.SwipeAdp;
import sse.ustc.cc.custom.CustomSwipeListView;
import sse.ustc.cc.db.DBService;
import sse.ustc.cc.util.ClassInfo;
import sse.ustc.cc.util.WXMessage;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 已选课程fragment
 * @author xuhao
 *
 */
public class ChoosedCourseFrag extends Fragment {

	private Context mContext;
	//每一个listview item左端显示的数据
	private List<WXMessage> data = new ArrayList<WXMessage>();
	private ArrayList<String> courseList;
	private CustomSwipeListView mListView;//自定义listview组件
	private SwipeAdp mAdapter;//自定义listview组件的数据适配器
	private ArrayList<ClassInfo> classList;

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		mContext = activity;
		// DBService ds = new DBService(this);
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		courseList = new ArrayList<String>();
		//从数据库中查询选修的课程，并装载到courseList中
		DBService ds = new DBService(mContext);
		courseList = new ArrayList<String>();
		courseList = ds.selectCourse();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// 关联布局文件
		View rootView = inflater.inflate(R.layout.mycourse_item, container,
				false);
		// 绑定Layout里面的ListView inflater
		mListView = (CustomSwipeListView) rootView.findViewById(R.id.listview);
		//初始化数据
		initData();
		//声明数据适配器，并绑定数据
		mAdapter = new SwipeAdp(inflater.getContext(), data,
				mListView.getRightViewWidth());
		// 添加左滑按钮响应事件
		mAdapter.setOnRightItemClickListener(new SwipeAdp.onRightItemClickListener() {

			public void onRightItemClick(int flag, View v, int position) {
				
				if (flag == 0) {
					Drawable clock = getResources().getDrawable(R.drawable.clock);
					Drawable clocked = getResources().getDrawable(R.drawable.clocked);
					int alarm_time = Integer.parseInt(data.get(position).getCourse_alarm());
					if(alarm_time < 0){
						Toast.makeText(getActivity(), "提醒" + (position + 1),
								Toast.LENGTH_SHORT).show();
						((TextView)v).setBackgroundDrawable(clocked);
						data.get(position).setCourse_alarm("15");
					}else{
						Toast.makeText(getActivity(), "取消提醒" + (position + 1),
								Toast.LENGTH_SHORT).show();
						((TextView)v).setBackgroundDrawable(clock);
						data.get(position).setCourse_alarm("-1");
					}
					
				} else if (flag == 1) {
					Toast.makeText(getActivity(), "删除" + (position + 1),
							Toast.LENGTH_SHORT).show();
					DBService ds = new DBService(mContext);
					Log.e("data", data.get(position).getTitle());
					ds.deletCourse(data.get(position).getTitle());
					data.remove(position);
					mAdapter.notifyDataSetChanged();

				}

			}

		});

		mListView.setAdapter(mAdapter);
		//点击某一行item响应事件
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getActivity(), "item onclick " + position,
						Toast.LENGTH_SHORT).show();
			}
		});

		return rootView;
	}
	
	/**
	 * 初始化数据
	 */
	private void initData() {
		WXMessage msg = null;
		data.clear();
		int j = courseList.size() / 13;
		Log.e("data.size()", data.size() + "");

		for (int i = 0; i < j; i++) {
			msg = new WXMessage(courseList.get(13 * i), courseList.get(13 * i + 5));
			data.add(msg);

		}
	}

}
